package com.fr.base.core;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.data.core.db.YearDate;
import com.fr.util.Utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtils
{
  public static final DateFormat DATEFORMAT1 = new SimpleDateFormat("yyyy/MM/dd");
  public static final DateFormat DATEFORMAT2 = new SimpleDateFormat("yyyy-MM-dd");
  public static final DateFormat DATEFORMAT3 = new SimpleDateFormat("dd/MM/yyyy");
  public static final DateFormat DATEFORMAT4 = new SimpleDateFormat("MM/dd/yyyy");
  public static final DateFormat DATETIMEFORMAT1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  public static final DateFormat DATETIMEFORMAT2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  public static final DateFormat DATETIMEFORMAT3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  public static final DateFormat DATETIMEFORMAT4 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
  public static final DateFormat TIMEFORMAT = new SimpleDateFormat("HH:mm:ss");
  private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
  public static final String SECOND = "s";
  public static final String MINUTE = "m";
  public static final String HOUR = "h";
  public static final String DAY = "d";
  public static final String WEEK = "w";

  public static synchronized String getDate2Str(String paramString, java.util.Date paramDate)
  {
    simpleDateFormat.applyPattern(paramString);
    return simpleDateFormat.format(paramDate);
  }

  public static String getDate2LStr(java.util.Date paramDate)
  {
    return getDate2Str("yyyy-MM-dd HH:mm:ss", paramDate);
  }

  public static String getDate2AllIncludeSSS(java.util.Date paramDate)
  {
    return getDate2Str("yyyyMMddHmmssSSS", paramDate);
  }

  public static java.util.Date createDate(int paramInt1, int paramInt2, int paramInt3)
  {
    return createDate(paramInt1, paramInt2, paramInt3, 0, 0, 0);
  }

  public static java.util.Date createDate(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6)
  {
    Calendar localCalendar = Calendar.getInstance();
    localCalendar.setTimeInMillis(0L);
    localCalendar.set(1, paramInt1);
    localCalendar.set(2, paramInt2 - 1);
    localCalendar.set(5, paramInt3);
    localCalendar.set(11, paramInt4);
    localCalendar.set(12, paramInt5);
    localCalendar.set(13, paramInt6);
    return localCalendar.getTime();
  }

  public static java.util.Date object2Date(Object paramObject, boolean paramBoolean)
  {
    java.util.Date localDate = null;
    if (paramObject instanceof java.util.Date)
    {
      localDate = (java.util.Date)paramObject;
    }
    else if (paramObject instanceof Calendar)
    {
      localDate = ((Calendar)paramObject).getTime();
    }
    else if (paramObject instanceof Number)
    {
      long l = ((Number)paramObject).longValue();
      if (l > 1000000L)
        localDate = new java.util.Date(l);
      else
        localDate = new java.util.Date((l - 25569L) * 1000L * 24L * 60L * 60L);
    }
    else if (paramObject != null)
    {
      localDate = string2Date(Utils.objectToString(paramObject), paramBoolean);
    }
    if ((!(paramBoolean)) && (localDate == null))
      localDate = new java.util.Date();
    return localDate;
  }

  public static java.util.Date string2Date(String paramString, boolean paramBoolean)
  {
    if (StringUtils.isBlank(paramString))
      return null;
    if (paramString.matches("^\\d+$"))
      return object2Date(Long.valueOf(paramString), true);
    if (paramString.matches("\\d{1,2}/\\d{1,2}/\\d{4}"));
    try
    {
      return DATEFORMAT4.parse(paramString);
    }
    catch (ParseException localParseException2)
    {
      FRContext.getLogger().log(Level.WARNING, paramString + " can't be parsed to Date");
      try
      {
        return DATETIMEFORMAT1.parse(paramString);
      }
      catch (ParseException localParseException2)
      {
        try
        {
          return DATETIMEFORMAT2.parse(paramString);
        }
        catch (ParseException localParseException3)
        {
          try
          {
            return DATETIMEFORMAT3.parse(paramString);
          }
          catch (ParseException localParseException4)
          {
            try
            {
              return DATETIMEFORMAT4.parse(paramString);
            }
            catch (ParseException localParseException5)
            {
              try
              {
                return DATEFORMAT1.parse(paramString);
              }
              catch (ParseException localParseException6)
              {
                try
                {
                  return DATEFORMAT2.parse(paramString);
                }
                catch (ParseException localParseException7)
                {
                  try
                  {
                    return DATEFORMAT3.parse(paramString);
                  }
                  catch (ParseException localParseException8)
                  {
                    if (paramBoolean)
                      return null;
                    FRContext.getLogger().log(Level.WARNING, paramString + " can't be parsed to Date");
                  }
                }
              }
            }
          }
        }
      }
    }
    return new java.util.Date();
  }

  public static long subtractDate(java.util.Date paramDate1, java.util.Date paramDate2, String paramString)
  {
    long l1 = paramDate1.getTime();
    long l2 = paramDate2.getTime();
    long l3 = l1 - l2;
    if ("s".equals(paramString))
      return (l3 / 1000L);
    if ("m".equals(paramString))
      return (l3 / 60000L);
    if ("h".equals(paramString))
      return (l3 / 3600000L);
    if ("d".equals(paramString))
      return (l3 / 86400000L);
    if ("w".equals(paramString))
      return (l3 / 604800000L);
    return (l3 / 86400000L);
  }

  public static java.util.Date datePlusInteger(java.util.Date paramDate, int paramInt)
  {
    long l = paramDate.getTime();
    l += paramInt * 24L * 3600L * 1000L;
    return new java.util.Date(l);
  }

  public static YearDate date2Year(Object paramObject)
  {
    YearDate localYearDate = null;
    if (paramObject instanceof String)
    {
      java.util.Date localDate = string2Date(paramObject.toString(), true);
      if (localDate != null)
        paramObject = localDate;
      else if (Pattern.compile("^\\d{4}").matcher((String)paramObject).find())
        localYearDate = new YearDate(((String)paramObject).substring(0, 4));
    }
    if (paramObject instanceof java.util.Date)
      localYearDate = new YearDate((java.util.Date)paramObject);
    else if (paramObject instanceof java.sql.Date)
      localYearDate = new YearDate((java.sql.Date)paramObject);
    return localYearDate;
  }
}